package com.tcsl.mylibrary;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.tcsl.mylibrary.utils.WPPayUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * 描述:
 * <p/>作者：wjx
 * <p/>创建时间: 2017/5/26 16:44
 */
public class PayWangPos extends TcslPay {
    private Activity mActivity;
    private BizServiceInvoker wpwizServiceInvoker;
    private BizServiceInvoker.OnResponseListener mOnResponseListener;

    public PayWangPos(Context context) {
        super(context);
        try {
            mActivity = (Activity) context;
        } catch (Exception e) {
            throw new IllegalArgumentException("the class must be the children of Activity");
        }
        init();
    }

    private void init() {
        wpwizServiceInvoker = WeiposImpl.as().openBizServiceInvoker();
        mOnResponseListener = new BizServiceInvoker.OnResponseListener() {
            @Override
            public void onResponse(String sdCode, String token, byte[] data) {
                // 收银服务调用完成后的返回方法
                Log.i(TAG, "wp onResponse" + "sdCode = " + sdCode + " , token = " + token + " , data = " + new String(data));
                try {
                    String result = new String(data);
                    JSONObject obj = new JSONObject(result);
                    String code = obj.getString("errCode");
                    String msg = obj.getString("errMsg");
                    if ("0".equals(code)) {//支付成功
                        mListener.paySuccess("旺POS支付成功", "");
                    } else {
                        mListener.payError(msg);
                    }
                } catch (JSONException e) {
                    mListener.payError("数据出现异常");
                }
            }

            @Override
            public void onFinishSubscribeService(boolean b, String s) {
                // bp订阅收银服务返回结果
                Log.d(TAG, "wp onResponse onFinishSubscribeService");
            }
        };
    }

    @Override
    protected void pay(Float money, String orderNo, String merchantNo) {
        cleanWp(money, orderNo);
    }

    /**
     * 清除旺POS未处理的订单和正在处理的订单清空
     *
     * @param money
     * @param orderNo
     */
    private void cleanWp(final Float money, final String orderNo) {
        BizServiceInvoker.OnResponseListener cleanListener = new BizServiceInvoker.OnResponseListener() {
            @Override
            public void onResponse(String s, String s1, byte[] bytes) {
                requestWp(money, orderNo);
            }

            @Override
            public void onFinishSubscribeService(boolean b, String s) {
            }
        };
        try {
            WPPayUtil.clean(wpwizServiceInvoker, pkName, cleanListener);
        } catch (UnsupportedEncodingException e) {
            mListener.notSupport();
        }
    }

    /**
     * 请求消费
     *
     * @param money
     * @param orderNo
     */
    private void requestWp(Float money, String orderNo) {
        String name = mActivity.getClass().getName();
        try {
            WPPayUtil.consume(wpwizServiceInvoker, mOnResponseListener, money, pkName, name, orderNo);
        } catch (Exception e) {
            mListener.notSupport();
        }
    }

}
